package com.java.permission.module.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;

import com.java.permission.common.dao.entity.SysUser;
import com.java.permission.common.dto.SysUserDto;
import com.java.permission.common.params.PageParams;
import com.java.permission.common.service.SysUserService;
import com.java.permission.common.utils.UserUtils;
import com.java.permission.common.vo.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

import java.util.List;



/**
 * @author hu
 * @version 1.0
 * @date 2020/6/12 16:38
 * @description
 */
@RestController
@RequestMapping("/sys/user")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    /**
     * 保存 用户
     * @param sysUserDto
     * @return
     */
    @RequestMapping("/saveOrUpdate")
    public Result saveOrUpdate(@Valid  SysUserDto sysUserDto) {
        sysUserService.validUserParams(sysUserDto);
        if (StringUtils.isNotBlank(sysUserDto.getId())) {
            sysUserService.edit(sysUserDto, UserUtils.getUserId());
        } else {
            sysUserService.save(sysUserDto, UserUtils.getUserId());
        }
        return Result.success(null);
    }


    /**
     * 查询用户集合
     * @return
     */
    @RequestMapping("/list")
    public Result list(){
        List<SysUser> sysUserList = sysUserService.findValidAll();
        return Result.success(sysUserList);
    }

    /**
     * 分页获取用户信息
     * @param pageParams
     * @return
     */
    @RequestMapping("/page")
    public Result findPageBy(PageParams pageParams) {
        IPage<SysUser> sysUserPage = sysUserService.findValidPageBy(pageParams);
        return  Result.success(sysUserPage);
    }
}
